package view;

import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;


import model.*;
import model.database.CalendarWrapper;
import model.schedule.Ad;
import model.schedule.Schedule;
import model.schedule.Scheduler;

public class ScheduleListener implements Observer {
	public ScheduleListener(Scheduler scheduler) {
		scheduler.addObserver(this);
	}

	public void update(Observable arg0, Object arg1) {
		Scheduler scheduler = (Scheduler) arg0;
		if (arg1 instanceof Schedule) {
			Schedule schedule = (Schedule) arg1;
			System.out.println("New schedule created:");

			Iterator<Ad> itr = schedule.iterator();
			CalendarWrapper timeNow = null;
			while (itr.hasNext()) {
				Ad auction = itr.next();
				timeNow = new CalendarWrapper();
				System.out
						.println("Sleep: "
								+ (auction.getEnd().timeMillis() - timeNow
										.timeMillis()) + "ms");
				System.out.println("Handle auction: " + auction.getId());
			}

			System.out.println();
		} else if (arg1 instanceof String) {
			String message = (String) arg1;
			System.out.println("Schedule " + (new CalendarWrapper()) + ": "
					+ message);
		}
	}
}
